package com.fastCms.manager.bim.controller;

import com.fastCms.business.bim.pojo.entity.Workduce;
import com.fastCms.business.bim.pojo.po.WorkduceListPO;
import com.fastCms.business.bim.pojo.po.WorkducePO;
import com.fastCms.business.bim.service.WorkduceService;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.business.frame.response.ResponseUtil;
import com.fastCms.business.frame.utils.CommonsUtil;
import com.fastCms.manager.frame.annotation.UserAccess;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author : iron
 * @version : 1.0.0
 * @date : 9:17 PM 7/6/21
 */
@RestController
@RequestMapping("/workduce")
@Api(value = "WorkduceController", tags = "后台-工序管理")
@UserAccess
public class WorkduceController {

    @Autowired
    private WorkduceService workduceServiceImpl;

    @ApiOperation(value = "工序列表", notes = "")
    @PostMapping("/workduceList")
    public ApiResponse<Workduce> workduceList(@RequestBody WorkduceListPO workducePO) {
        List<Workduce> workduces = workduceServiceImpl.findValidAll(workducePO);
        return ResponseUtil.success(workduces);
    }

    @ApiOperation(value = "保存工艺", notes = "")
    @PostMapping("/saveWorkduce")
    public ApiResponse<Boolean> saveWorkduce(@RequestBody WorkducePO workducePO) {
        if (Objects.isNull(workducePO.getId())) {
            Workduce workduce = new Workduce();
            CommonsUtil.initEntity(workduce);
            BeanUtils.copyProperties(workducePO, workduce);
            Boolean result = workduceServiceImpl.save(workduce);
            return ResponseUtil.success(result);
        }
        Workduce workduce = workduceServiceImpl.findById(workducePO.getId());
        BeanUtils.copyProperties(workducePO, workduce);
        Boolean result = workduceServiceImpl.updateById(workduce);
        return ResponseUtil.success(result);
    }

    @ApiOperation(value = "删除", notes = "")
    @GetMapping("/delele/{id}")
    public ApiResponse<Boolean> delete(@PathVariable Integer id) {
        boolean b = workduceServiceImpl.removeById(id);
        return ResponseUtil.success(b);
    }
}
